#include "px_animationdata.h"
#include "../utility/px_filebin.h"
static void _copy_frame(PXFrame * dest,PXFrame * src)
{
	 dest->attack_rects = src->attack_rects;
	 dest->attack_rects_len =src->attack_rects_len;
	 dest->id = src->id;
	 dest->module_len = dest->module_len;
	 dest->modules = src->modules;
}
static PXRectangle _read_rectangle(FileBin * fbin)
{
	PXRectangle rect;
	rect.x = readInt(fbin);
	rect.y = readInt(fbin);
	rect.width = readInt(fbin);
	rect.height = readInt(fbin);
	return rect;
}
static PXModule  _read_module(FileBin * fbin)
{
	PXModule ret;
	ret.id = readInt(fbin);
	ret.image_id = readInt(fbin);
	ret.module_rect = _read_rectangle(fbin);
	return ret;
}
static PXMoudleInFrame _read_moudle_inframe(FileBin * fbin)
{
	PXMoudleInFrame ret;
	ret.moduleId = readInt(fbin);
	ret.x = readInt(fbin);
	ret.y = readInt(fbin);
	return ret;
}
static PXFrame _read_frame(FileBin * fbin)
{
	PXFrame ret;
	int i;
	ret.id = readInt(fbin);
	ret.module_len = readInt(fbin);
	ret.modules= (PXMoudleInFrame *) malloc(sizeof(PXMoudleInFrame)*ret.module_len);
	for(i = 0 ; i< ret.module_len ;i++)
	{
		ret.modules[i] = _read_moudle_inframe(fbin);
	}
	ret.attack_rects_len = readInt(fbin);
	ret.attack_rects = (PXRectangle *)malloc(sizeof(PXRectangle)*ret.attack_rects_len);
	for( i = 0 ;i < ret.attack_rects_len;i++)
	{
		ret.attack_rects[i] = _read_rectangle(fbin);
	}
	return ret;
}

static PXFrameInAction _read_frameinaction(FileBin * fbin)
{
	PXFrameInAction ret;
	ret.frameId = readInt(fbin);
	ret.delay = readInt(fbin);
	ret.x = readInt(fbin);
	ret.y = readInt(fbin);
	return ret;
}

static PXAction _read_action(FileBin * fbin)
{
	PXAction ret;
	int i;
	ret.id =readInt(fbin);
	ret.frame_len = readInt(fbin);
	ret.frames = (PXMoudleInFrame *) malloc(sizeof(PXMoudleInFrame)*ret.frame_len);
	for(i=0 ;i < ret.frame_len;i++)
	{
		ret.frames[i] = _read_frameinaction(fbin);
	}
	return ret;
}
static PXAniImage _read_image(FileBin * fbin)
{
	PXAniImage ret;
	ret.id = readInt(fbin);
	ret.image_names = readString_1(fbin);
	return ret;
}


PXAnimationData * load_animation( const char * fileName )
{
	PXAnimationData * ret = (PXAnimationData * )malloc(sizeof(PXAnimationData));
	int count,i;
	FileBin* fbin = init_fileBin(fileName);
	count = readInt(fbin);
	ret->image_count = count;
	ret->images =(PXAniImage*) malloc(sizeof(PXAniImage)*count);
	for(i =0 ;i < count ; i ++)
	{
		ret->images[i] = _read_image(fbin);
	}
	count = readInt(fbin);
	ret->modules = (PXModule *)malloc(sizeof(PXModule)*count);
	for(i = 0 ;i < count ;i++)
	{
		ret->modules[i]= _read_module(fbin);
	}
	count = readInt(fbin);
	ret->frames = (PXFrame *)malloc(sizeof(PXFrame)*count);
	for(i=0 ;i< count ;i++)
	{
		 ret->frames[i] = _read_frame(fbin);	
	}
	count = readInt(fbin);
	ret->actions = (PXAction *)malloc(sizeof(PXAction)*count);
	for (i = 0 ; i< count; i++)
	{
		ret->actions[i] = _read_action(fbin);
	}
	return ret;
}
